package com.itheima.controller;

import com.itheima.common.PageResult;
import com.itheima.dto.Simple;
import com.itheima.dto.SubjectsTagsDto;
import com.itheima.pojo.SubjectsTags;
import com.itheima.pojo.User;
import com.itheima.service.SubjectsTagsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/tags")
public class SubjectsTagsController {

    @Autowired
    private SubjectsTagsService subjectsTagsService;

    @Autowired
    private HttpServletRequest req;

    /**
     *
     * Description:
     *标签添加
     * @param:  * @param subjectsTags
     * @return: {@link String}
     */
    @PostMapping
    public String addSubjectsTags(@RequestBody SubjectsTags subjectsTags){

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        subjectsTags.setAddDate(LocalDateTime.now());
        subjectsTags.setCreatorID(1);
        Integer id = subjectsTagsService.addSubjectsTags(subjectsTags);

        return "{'id':"+id+"}";
    }

    /**
     *
     * Description:
     *标签修改
     * @param:  * @param id
     * @param subjectsTags
     * @return: {@link String}
     */
    @PutMapping("/{id}")
    public String modifySubjectsTags(@PathVariable Integer id,@RequestBody SubjectsTags subjectsTags){

        subjectsTagsService.modifySubjectsTags(subjectsTags);

        return "{'success':true}";

    }

    /**
     *
     * Description:
     *标签列表
     * @param:  * @param page
     * @param pagesize
     * @param subjectID
     * @param tagName
     * @param state
     * @return: {@link PageResult}
     */
    @GetMapping
    public PageResult SubjectsTagsPage(Integer page, Integer pagesize, Integer subjectID, String tagName, Integer state){
        return subjectsTagsService.SubjectsTagsPage(page,pagesize,subjectID,tagName,state);
    }

    /**
     *
     * Description:
     *标签删除
     * @param:  * @param id
     * @param subjectsTags
     * @return: {@link String}
     */
    @DeleteMapping("/{id}")
    public String removeSubjectsTagsById(@PathVariable Integer id,@RequestBody SubjectsTags subjectsTags){
        subjectsTagsService.removeSubjectsTagsById(id);

        return "{'success':false}";
    }

    /**
     *
     * Description:
     *标签修改
     * @param:  * @param id
     * @param state
     * @return: {@link String}
     */
    @PostMapping("/{id}/{state}")
    public String modifySubjectsTagsState(@PathVariable Integer id,@PathVariable Integer state){
        subjectsTagsService.modifySubjectsTagsState(id,state);
        return "{'success':true}";

    }

    /**
     *
     * Description:
     *标签详情
     * @param:  * @param id
     * @return: {@link SubjectsTags}
     */
    @GetMapping("/{id}")
    public SubjectsTagsDto findSubjectsTagsById(@PathVariable Integer id){
       return subjectsTagsService.findSubjectsTagsById(id);
    }


    @GetMapping("/simple")
    public List<Simple> subjectsTagsSimple(Integer subjectID){
        return subjectsTagsService.subjectsTagsSimple(subjectID);
    }

}
